package ex3;

import ex3.visitors.StructureCheckVisitor;
import ex3.visitors.SymbolTableVisitor;
import ex3.visitors.TypeCheckVisitor;
import ic.ast.PrettyPrint;
import ic.ast.decl.Program;
import ic.symbols.GlobalSymbolTable;

/**
 * Created with IntelliJ IDEA.
 * User: Roi
 * Date: 12/5/13
 */
public class SemanticAnalyzer extends Ex3Runnable {

    private SemanticAnalyzer() {}

    @Override
    public void run() throws Exception {
        Program ast = getAst();

        GlobalSymbolTable globalSymbolTable = (GlobalSymbolTable)ast.accept(new SymbolTableVisitor());

        StructureCheckVisitor visitor = new StructureCheckVisitor();
        ast.accept(visitor);
        globalSymbolTable.accept(visitor);

        ast.accept(new TypeCheckVisitor());

        System.out.println(globalSymbolTable.accept(new PrettyPrint()));
    }

    /**
     * Builder inner class
     */
     public static class Builder {
        private SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();

        public Builder withProgramFilename(String programFilename) {
            semanticAnalyzer.programFilename = programFilename;
            return this;
        }

        public Builder withLibraryFilename(String libraryFilename) {
            semanticAnalyzer.libraryFilename = libraryFilename;
            return this;
        }

        public SemanticAnalyzer build() {
            return semanticAnalyzer;
        }
    }
}
